using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using OpenMTS.Properties;

namespace OpenMTS
{
    public partial class RunDialog : Form
    {
        private Hashtable ht = new Hashtable();
        private string[] arrRulesets = null;
        private string sInputMessage = "";

        public RunDialog(ProjectFile project)
        {
            InitializeComponent();

            //Only one message last used for now
            string sMessage = "";
            if (Settings.Default.RecentMessages != null)
            {
                sMessage = Settings.Default.RecentMessages;

            }
            //Set the input message combo values
            ProjectItem[] messages = project.getMessageFileList();
            
            foreach (ProjectItem sFile in messages)
            {
                int i = this.cmbInputMessage.Items.Add(sFile);
                if (sMessage == sFile.FileName) this.cmbInputMessage.SelectedIndex = i;
                else
                    this.cmbInputMessage.SelectedIndex = 0;
            
            }
            

            //Get the last set of rulesets used so we can see if this project has them
            //and check the dialog for the user
            string[] sRulesets = new string[0];
            if (Settings.Default.RecentRulesets != null)
            {
                sRulesets = Settings.Default.RecentRulesets.Split(',');

            }
            ProjectItem[] rules = project.getRulesetFileList();
            foreach (ProjectItem rule in rules)
            {
                bool bCheck = false;
                foreach (string sRule in sRulesets)
                {
                    if (sRule == rule.FileName)
                    {
                        bCheck = true;
                        break;
                    }
                }
                //Add ruleset to the listbox with a check if it was 
                this.lbRulesets.Items.Add(rule.FileName, bCheck);
                ht.Add(rule.FileName, rule);               

            }

            //TODO: Add custom report stuff
            
            cmbReportFiles.Items.Add("Default");
            cmbReportFiles.SelectedIndex = 0;
           
            if (Settings.Default.ViewAsXml)
                this.rbXml.Checked = true;
            else
                this.radioButton2.Checked = true;
            

            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {
            if (this.lbRulesets.CheckedItems.Count == 0)
            {
                MessageBox.Show("You must check at least 1 ruleset file.");                
                return;
            }

            Settings.Default.RecentRulesets = "";
            arrRulesets = new string[this.lbRulesets.CheckedItems.Count];
            int i = 0;
            foreach (string sRule in this.lbRulesets.CheckedItems)
            {
                Settings.Default.RecentRulesets += sRule + ",";
                ProjectItem pi = (ProjectItem)ht[sRule];      
                arrRulesets[i++] = pi.Path + sRule; 
            }
            Settings.Default.RecentRulesets.TrimEnd(',');

            ProjectItem proj = (ProjectItem)cmbInputMessage.Items[cmbInputMessage.SelectedIndex];
            sInputMessage = proj.Path + proj.FileName;
            Settings.Default.RecentMessages = proj.FileName;
            
            Settings.Default.ViewAsXml = this.rbXml.Checked;
            Settings.Default.Save();

            this.DialogResult = DialogResult.OK;
        }

        public string[] Rulesets
        {
            get { return arrRulesets; }
        }

        public string InputMessage
        {
            get { return sInputMessage; }
        }
    }
}